package com.babyqiao.wallpaper.manager.controller;

import com.babyqiao.wallpaper.manager.controller.dto.AddCategoryParams;
import com.babyqiao.wallpaper.manager.entity.WallpaperCategory;
import com.babyqiao.wallpaper.manager.entity.vo.CategoryVO;
import com.babyqiao.wallpaper.manager.service.CateagryService;
import com.babyqiao.wallpaper.common.UnifyResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理接口
 *
 * @author qinglongzhai
 * @date 2020/12/12
 */
@Api(tags = "分类管理接口")
@RestController
@RequestMapping("/api/admin/categorys")
public class CategoryController {
    @Autowired
    private CateagryService cateagryService;

    /***
     * 添加分类
     * @return UnifyResult
     */
    @ApiOperation(value = "添加分类", httpMethod = "POST")
    @PostMapping("/category")
    public UnifyResult addCategory(@ApiParam(name = "addCategoryParams", value = "分类信息", required = true) @RequestBody AddCategoryParams addCategoryParams) {
        boolean state = cateagryService.saveCategory(addCategoryParams);
        if (state) {
            return UnifyResult.success();
        }
        return UnifyResult.error().message("分类已存在");

    }

//    /***
//     * 分页查询分类
//     *
//     * @param current 当前页数
//     * @param limit 每页条数
//     * @return
//     */
//    @ApiOperation(value = "分页查询分类",httpMethod = "POST")
//    @PostMapping("/{current}/{limit}/categorys")
//    public UnifyResult getCategory(@ApiParam(name = "current",value = "当前页码",required = true) @PathVariable long current,
//                                   @ApiParam(name = "limit",value = "每页查询条数",required = true)@PathVariable long limit,
//                                   @ApiParam(name = "addCategoryParams",value = "查询条件",required = true)@RequestBody AddCategoryParams addCategoryParams){
//        QueryWrapper<WallpaperCategory> queryWrapper=cateagryService.addSelectCondition(addCategoryParams);
//
//
//        Page<WallpaperCategory> page=new Page<>(current,limit);
//        cateagryService.page(page, queryWrapper);
//
//        long total = page.getTotal();
//        List<WallpaperCategory> records = page.getRecords();
//
//        return UnifyResult.success().data("total",total).data("result",records);
//    }

    /**
     * 查询分类
     *
     * @return 整理好的分类集合
     */
    @ApiOperation(value = "查询分类", httpMethod = "GET")
    @GetMapping("/categorys")
    public UnifyResult getAllCategory() {
        List<CategoryVO> allcategory = cateagryService.getAllcategory();

        return UnifyResult.success().data("result", allcategory);
    }

    /**
     * 更新分类
     *
     * @param wallpaperCategory
     * @return 是否成功
     */
    @ApiOperation(value = "更新分类", httpMethod = "PUT")
    @PutMapping("/category")
    public UnifyResult updateCategory(@RequestBody WallpaperCategory wallpaperCategory) {
        boolean b = cateagryService.updateById(wallpaperCategory);
        return UnifyResult.success();

    }

    /**
     * 删除分类
     *
     * @param ids
     * @return
     */
//    @ApiOperation(value = "删除分类",httpMethod = "POST")
//    @PostMapping("/subject/category")
//    public UnifyResult deleteCategory(@ApiParam(name = "id",value = "要删除的分类id",required = true) @RequestBody Map<String,List<String>> params){
//        List<String>ids=params.get("ids");
//        boolean b = cateagryService.removeByIds(ids);
//        if(b){
//            return UnifyResult.success();
//        }
//        return UnifyResult.error();
//
//    }
    @ApiOperation(value = "删除分类", httpMethod = "DELETE")
    @DeleteMapping("/subject/category")
    public UnifyResult deleteCategory(@ApiParam(name = "ids", value = "要删除的分类id集合", required = true) @RequestParam(value = "ids") List<String> ids) {
        System.out.println(ids);

        boolean b = cateagryService.removeByIds(ids);
        if (b) {
            return UnifyResult.success();
        }
        return UnifyResult.error();

    }


}

